<template>
    <div class="login_container">
        <!-- 登录区域 -->
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt/>
            </div>
            <!-- 表单区域 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login_form" label-width="0">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="fa fa-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" prefix-icon="fa fa-key"></el-input>
                </el-form-item>
                <!-- 登录 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login()">登录</el-button>
                    <el-button type="info" @click="resetLoginForm()">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data(){
        return{
            // 表单数据
            loginForm:{
                username:"admin",
                password:"admin"
            },
            // 数据验证
            loginRules:{
                username:[
                    // required: 是否必填
                    // message: 提示
                    // min: 最短
                    // max: 最长
                    // trigger: 触发方式；blur: 失去焦点
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 4, max: 8, message: '长度在 4 到 8 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        resetLoginForm(){
            // 重置表单数据 this.$refs.[ref=].resetFields();
            this.$refs.loginFormRef.resetFields();
        },
        login(){
            // async 异步；await 等待
            this.$refs.loginFormRef.validate(async valid => {
                // valid 验证是否成功
                if(!valid) return;
                var dataEncrypt = this.$encrypt.encrypt(this.loginForm);
                // 获取返回数
                const {data:res} = await this.$http.post("/login",dataEncrypt);
                //console.log(res);
                if(res.flag == "OK"){
                    this.$message.success("登录成功！");
                    var result = this.$encrypt.decrypt(res);
                    window.sessionStorage.setItem("user",result);
                    this.$router.push({path:"/home"});
                }else{
                    this.$message.error("登录失败！");
                }
            });
        }
    }
}
</script>

<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #ffffff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    // 改变自己，移动(上50%,左50%)
    transform: translate(-50%,-50%);
    .avatar_box{
        width: 130px;
        height: 130px;
        border: 1px solid #eeeeee;
        border-radius: 50%;
        padding: 5px;
        box-shadow: 0 0 2px #dddddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #00eeee;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eeeeee;
        }
    }
}
.login_form{
    position: absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
}
.btns{
    // 位于容器的结尾
    display: flex;
    justify-content: flex-end;
}
</style>